<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', bomCost('unitsh:save') && 'newy', 'remove', 'pass', 'sub', 'option']"
      :disab-arr="['close', 'cancel', 'copy', 'freeze', 'com1', 'com2', 'chart']"
      @del="delCustom"
      @refresh="getUserList"
      @newly="addMateril"
      @toExcel="handleExport"
      @audit="Retrial(1)"
      @noAudit="Retrial(2)"
      @submit="materSub(1)"
      @noSubmit="materSub(2)"
      @forbidden="forbidden(1)"
      @startUsing="forbidden(2)"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="searchMaterialList"
        />
        <el-button size="mini" type="primary" class="btn" @click="searchMaterialList">
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="searchMaterialList"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        sortable
        :table-data="tableData"
        :table-header="tableHeader"
        table-selection
        get-row-keys="fentryid"
        @selectionChange="selectionDatas"
        @clickRow="detailCustomer"
      >
        <template v-slot:btnSlot="clo">
          <el-link
            type="primary"
            size="mini"
            @click="detailCustomer(clo.scope.row)"
          >
            {{ clo.scope.row.fnumber }}
          </el-link>
        </template>
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentstatus }}</el-tag>
        </template>
      </jc-table>
      <cardTab :tab-arr="tabArr" @search="handleQueryBomList" />
    </div>
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="pageNum"
        :limit.sync="size"
        @pagination="getUserList"
      />
    </div>
  </div>
</template>

<script>
import {
  deleteUnit, queryUnitconvertrateList, unitAudit, unitDisable,
  unitSubmit, unitUnAudit, unitUnDisable, unitUnSubmit
} from '@/api/basicManagement/customerBom'
import search from '@/components/Search/index'
import searData from '@/components/Search/mixin'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { toMxBmina } from '@/components/ToMxamineState'
export default {
  components: {
    search,
    cardTab: () => import('@/components/cardTab'),
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 50,
      fbillNo: 'fname',
      total: 0, // 总条目
      pageNum: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tabArr: [
        { label: '包材', name: '4-3' },
        { label: '泡沫', name: '3-2' },
        { label: '软体', name: '4-2' },
        { label: '管材', name: '3-1-01' },
        { label: '布', name: '3-1-02' },
        { label: '瓷砖', name: '3-1-08' },
        { label: '螺丝', name: '3-3-02' },
        { label: '弹簧', name: '3-3-08' }
      ],
      tableData: [], // 列表数据
      // 表头数据
      tableHeader: [
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('market.fmaterial'), type: 'btn', prop: 'fnumber' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecificaTion', width: '150' },
        { label: this.$t('market.baseUnit'), prop: 'fdestunitname' },
        { label: this.$t('market.molecule'), prop: 'fconvertnumerator', width: '80' },
        { label: this.$t('market.matrixingUnit'), prop: 'fcurrentunitname' },
        { label: this.$t('market.matrixing'), prop: 'fconvertdenominator', width: '80' },
        { label: this.$t('market.state'), type: 'state', prop: 'fdocumentStatus', width: '90' },
        { label: this.$t('market.creator'), prop: 'fcreator', width: '100' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate', width: '120' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.getUserList()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '物料换算列表')
        } else {
          queryUnitconvertrateList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return (toMxBmina(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '物料换算列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 物料换算列表
    async getUserList(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.pageNum,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: res } = await queryUnitconvertrateList(DATA)
      this.tableData = res.array.map(item => {
        return (toMxBmina(item))
      })
      this.total = res.total
      this.loading = false
    },
    // 搜索物料
    searchMaterialList(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.getUserList(ev.detail)
      } else {
        this.getUserList()
      }
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.pageNum = 1
      this.searCollData = ev
      this.searchSon = ev
      this.getUserList()
    },
    // 详情
    detailCustomer(ev) {
      const fid = ev.fid
      this.$router.push({ path: `/editConversion/${fid}` })
    },
    // 新增
    addMateril() {
      this.$router.push({ path: '/createConversion' })
    },
    selectionDatas(val) {
      this.selectFbi = []
      this.val = val
      this.val.forEach(res => {
        res.fid && this.selectFbi.push(res.fid + '')
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectFbi }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
        this.getUserList()
      })
    },
    // 提交
    async materSub(ev) {
      const api = ev === 1 ? unitSubmit : unitUnSubmit
      this.toExamineFun(api)
    },
    // 审核
    async Retrial(ev) {
      const api = ev === 1 ? unitAudit : unitUnAudit
      this.toExamineFun(api)
    },
    // 禁用
    async forbidden(ev) {
      const api = ev === 1 ? unitDisable : unitUnDisable
      this.toExamineFun(api)
    },
    // 删除
    delCustom() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = deleteUnit
        this.toExamineFun(api)
      })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>

<style scoped lang="scss">
.content {
  @include listBom;
  .table-content {
    margin-top: 5px;
  }
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
